package com.zg.mock.web;

import com.zg.common.cache.service.CacheService;
import com.zg.common.cache.service.impl.RedisCacheServiceImpl;
import com.zg.common.core.R;
import com.zg.common.lock.service.LockService;
import com.zg.mock.domain.dto.InvoiceBackDTO;
import com.zg.mock.domain.dto.InvoiceDTO;
import com.zg.mock.service.InvoiceService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RMapCache;
import org.redisson.api.RedissonClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * <p>Description: </p>
 *
 * @author zhanfeng.liang
 * @since 2025/5/19 10:15
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/mock/invoice")
@Slf4j
@Api(tags = "发票管理")
public class InvoiceController {

    private final CacheService cacheService;

    private final LockService lockService;

    private final InvoiceService invoiceService;
    @PostMapping("/accept")
    public R accept(@RequestBody InvoiceDTO invoiceDTO) {
        InvoiceBackDTO invoiceBackDTO =invoiceService.accept(invoiceDTO);
        invoiceService.asyncMethod(invoiceDTO, invoiceBackDTO);
//        cacheService.addKey("11", "22", 300L);
//        boolean locked = false;
//        try {
//            // 尝试获取锁，最多等待5秒，锁自动过期时间30秒
//            locked = lockService.tryLock("resourceLock", 5, 30, TimeUnit.SECONDS);
//            if (locked) {
//                // 获取锁成功，执行业务逻辑
//                Thread.sleep(2000); // 模拟业务处理
//                log.info("加锁成功，业务处理完成");
//            } else {
//                log.info("获取锁失败");
//            }
//        } catch (Exception e) {
//            log.error("处理异常", e);
//        }
//        finally {
//            if (locked) {
//                lockService.unlock("resourceLock");
//            }
//        }
        return  R.success();
    }
}
